package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityBurseRecord;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//可用钱包界面
public class AssetsAvailableMoneyAvtivity extends BaseActivity {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_burse_list)
    RecyclerView rvBurseList;
    IPresenter presenter;
    List<EntityBurseRecord> entityBurseRecordList;
    CommonAdapter<EntityBurseRecord> adapter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_assets_available_money;
    }

    public static Intent createIntent(Context context) {
        return new Intent(context, AssetsAvailableMoneyAvtivity.class);
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        entityBurseRecordList = new ArrayList<>();
        adapter = new CommonAdapter<EntityBurseRecord>(context, R.layout.item_burse_record_list, entityBurseRecordList) {
            @Override
            protected void convert(ViewHolder holder, EntityBurseRecord entityBurseRecord, int position) {
                holder.setText(R.id.remittanceuser, entityBurseRecord.getRemittanceUser());
                holder.setText(R.id.tv_create_time, entityBurseRecord.getCreateTime());
                holder.setText(R.id.tv_coin_count, entityBurseRecord.getCoinNumber() + "  china-A");
                ImageView ivType = holder.getView(R.id.iv_burse_type);
                if (entityBurseRecord.getCoinNumber() < 0) {
                    ivType.setImageResource(R.mipmap.ic_burse_record_out);
                } else {
                    ivType.setImageResource(R.mipmap.ic_burse_record_in);

                }

            }
        };
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                EntityBurseRecord record = entityBurseRecordList.get(position);
                startActivity(AssetsBurseDetailActivity.createIntent(context, record));

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

    }

    @Override
    public void initView() {
        tvTitle.setText("可用钱包");
        rvBurseList.setAdapter(adapter);
        rvBurseList.setLayoutManager(new LinearLayoutManager(context));
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void initOperator() {
        JSONObject param = new JSONObject();
        try {
            param.put("burseType", "20110001");
            param.put("pageindex", "1");
            param.put("pagesize", "1000");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.getUserBurseList(param.toString());
    }


    @Override
    public void onGetUserBurseListSuccess(List<EntityBurseRecord> entityBurseRecordList) {
        super.onGetUserBurseListSuccess(entityBurseRecordList);
        if (entityBurseRecordList != null && entityBurseRecordList.size() > 0) {
            this.entityBurseRecordList.clear();
            this.entityBurseRecordList.addAll(entityBurseRecordList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onGetUserBurseListFail(Exception e) {
        super.onGetUserBurseListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
